/**
 * ESP32-Arduino-Framework
 * Arduino开发环境下适用于ESP32芯片系列开发板的应用开发框架。
 * 
 * Author: Billy Zhang（billy_zh@126.com）
 */

#ifndef _XIAOZHI_APPLICATION_H
#define _XIAOZHI_APPLICATION_H

#include <cstring>

#include "../application.h"
#include "protocols/protocol.h"
#include "audio_service.h"
#include "wechat_window.h"

enum AecMode {
    kAecOff,
    kAecOnDeviceSide,
    kAecOnServerSide,
};

class XiaoZhiApplication : public Application {
public:
    XiaoZhiApplication();
    ~XiaoZhiApplication();
    
    void Init() override;
    void Start() override;
    void SetDeviceState(const DeviceState* state) override;
    bool CanEnterSleepMode() override;

    void Alert(const char* status, const char* message, const char* emotion, const char* sound);
    void AbortSpeaking(AbortReason reason);
    void ToggleChatState();
    void StartListening();
    void StopListening();
    void WakeWordInvoke(const std::string& wake_word);
    void SendMcpMessage(const std::string& payload);
    void SetAecMode(AecMode mode);
    AecMode GetAecMode() const { return aec_mode_; }
    void PlaySound(const std::string& sound);
    void ShowActivationCode(const std::string& code, const std::string& message);

    const std::string& GetAppName() const override { return "XiaoZhi"; }
    const std::string& GetAppVersion() const override { return "1.8.0"; }

private:
    void OnWakeWordDetected();
    void SetListeningMode(ListeningMode mode);

    ListeningMode listening_mode_ = kListeningModeAutoStop;
    std::unique_ptr<Protocol> protocol_;
    AudioService audio_service_;
    AecMode aec_mode_ = kAecOff;
    std::string last_error_message_;
    bool aborted_ = false;

    WechatWindow* window_ = nullptr;

};

#endif //_XIAOZHI_APPLICATION_H